package com.cool.shark.controller;

import com.cool.shark.pojo.dto.CartInsertDto;
import com.cool.shark.pojo.entity.Cart;
import com.cool.shark.service.CartService;
import com.cool.shark.web.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Name: CartController
 * @Author: 西风残叶
 * @Date: 2024/4/28 14:11
 * @Version: 1.0
 * @TODO: 购物车的控制器类
 */
@RestController
@Slf4j
@RequestMapping("/cart")
public class CartController {
    @Autowired
    private CartService service;
    @PostMapping("/insert")
    public JsonResult<Void> insert(@RequestBody CartInsertDto cartInsertDto) {
        log.info("开始向购物车添加商品,参数:{}", cartInsertDto);
        service.insert(cartInsertDto);
        log.info("向购物车添加商品成功");
        return JsonResult.ok();
    }
    @GetMapping("/list")
    public JsonResult<List<Cart>> list(@Param("userId")Integer userId) {
        log.info("开始获取购物车列表,参数:{}", userId);
        List<Cart> list = service.list(userId);
        log.info("获取购物车列表成功");
        return JsonResult.ok(list);
    }
    @GetMapping("/cartNum")
    public JsonResult<List<com.cool.shark.pojo.vo.cartNumVo>> list2(@Param("userId")Long userId) {
        log.info("开始获取购物车数量,参数:{}", userId);
        List<com.cool.shark.pojo.vo.cartNumVo> list = service.cartNumVo(userId);
        log.info("获取购物车数量成功");
        return JsonResult.ok(list);
    }
    @GetMapping("/delete")
    public JsonResult<Void> delete(@Param("spuId")Long spuId,@Param("userId")Integer userId) {
        log.info("开始删除购物车商品,参数:userId = {},spuId={}",userId,spuId);
        service.delete(spuId,userId);
        log.info("删除购物车商品成功");
        return JsonResult.ok();
    }
}
